package com.coder.dao;

import java.sql.Time;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.coder.model.DayIsa;
import com.coder.model.Isa;
import com.coder.model.MonthIsa;

public class IsaDao {
	
	private SessionFactory sessionFactory;
	
	public Session getSession() {
		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (Exception e) {
			session = sessionFactory.openSession();
		}
    	return session;
	}
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@SuppressWarnings("unchecked")
	public List<Isa> getIsaByDay(Date day){
		Session session = getSession();
		Query query = null;
		try{
			query = session.createQuery("from Isa where CDate=?")
					.setDate(0, day);
		}catch(Exception e){
			e.printStackTrace();
		}
		if(query.list().size()==0){
			return null;
		}else{
			return query.list();
		}
	}
	
	/**查询某一天的Isa的平均值*/
	@SuppressWarnings("unchecked")
	public List<DayIsa> getDayIsaByDay(Date day) {
		Session session = getSession();
		Query query = null;
		try{
			query = session.createQuery("from DayIsa where day=?")
					.setDate(0, day);
		}catch(Exception e){
			e.printStackTrace();
		}
		if(query.list().size()==0){
			return null;
		}else{
			return query.list();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Isa> getDataBetweenDate(Date dBefore, Time tBefore,
			Date dAfter, Time tAfter) {
		Session session = getSession();
		Query query = null;
		try{
			
			if(dBefore.compareTo(dAfter) != 0 ){
				query = session.createQuery("from Isa where (CDate=? and CTime>=?) or (CDate=? and CTime<=?) or (CDate>? and CDate<?)")
						.setDate(0, dBefore)
						.setTime(1, tBefore)
						.setDate(2, dAfter)
						.setTime(3, tAfter)
						.setDate(4, dBefore)
						.setDate(5, dAfter);
			}else{
				query = session.createQuery("from Isa where CDate=? and CTime>=? and CTime<=?")
						.setDate(0, dBefore)
						.setTime(1, tBefore)
						.setTime(2, tAfter);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		if(query.list().size() == 0){
			return null;
		}else{
			return query.list();
		}
	}

	public DayIsa saveDayIsa(DayIsa dayIsa) {
		Session session = getSession();
		try{
			session.saveOrUpdate(dayIsa);
		}catch(Exception e){
			e.printStackTrace();
		}
		return dayIsa;
	}

	@SuppressWarnings("unchecked")
	public List<DayIsa> getDayIsaByMonth(String month) {
		Session session = getSession();
		Query query = null;
		try{
			query = session.createQuery("from DayIsa where monthStr=?")
					.setString(0, month);
		}catch(Exception e){
			e.printStackTrace();
		}
		if(query.list().size()==0){
			return null;
		}else{
			return query.list();
		}
	}
	public MonthIsa saveMonthIsa(MonthIsa monthIsa) {
		Session session = getSession();
		try{
			session.saveOrUpdate(monthIsa);
		}catch(Exception e){
			e.printStackTrace();
		}
		return monthIsa;
	}
	
	@SuppressWarnings("unchecked")
	public List<MonthIsa> getMonthIsaByMonth(String month) {
		Session session = getSession();
		Query query = null;
		try{
			query = session.createQuery("from MonthIsa where monthStr=?")
					.setString(0, month);
		}catch(Exception e){
			e.printStackTrace();
		}
		if(query.list().size()==0){
			return null;
		}else{
			return query.list();
		}
	}

}
